using UnityEngine;

namespace IQIGame.USDK
{
    public class IQIUSDKImpl_Android : MonoBehaviour, IQIUSDKInterface
    {
        private AndroidJavaObject mJavaObject;

        void Awake()
        {
            var jc = new AndroidJavaClass("com.iqigame.libiqiusdkbridge.IQIUSdkWrapUnity");
            var jo = jc.GetStatic<AndroidJavaObject>("currentInstance");
            mJavaObject = jo;
        }

        public void init()
        {
            mJavaObject.Call("init");
        }

        public void login()
        {
            mJavaObject.Call("login");
        }

        public void logout()
        {
            mJavaObject.Call("logout");
        }

        public void exit()
        {
            mJavaObject.Call("exit");
        }

        public void switchAccount()
        {
            mJavaObject.Call("switchAccount");
        }

        public bool isSupportExitDialog()
        {
            return mJavaObject.Call<bool>("isSupportExitDialog");
        }

        public bool isSupportSwitchAccount()
        {
            return mJavaObject.Call<bool>("isSupportSwitchAccount");
        }

        public void pay(PaymentParameters orderInfo, RoleInfo roleInfo)
        {
            var orderStr = JsonUtility.ToJson(orderInfo);
            var roleStr = JsonUtility.ToJson(roleInfo);
            mJavaObject.Call("pay", orderStr, roleStr);
        }

        public void extraFunc(ExtraFuncType type, string jsonData)
        {
            mJavaObject.Call("extraFunc", (int)type, jsonData);
        }

        public void setAgreeProtocol()
        {
            mJavaObject.Call("setAgreeProtocol");
        }


        public void enterGame(RoleInfo roleInfo)
        {
            var str = JsonUtility.ToJson(roleInfo);
            mJavaObject.Call("enterGame", str);
        }

        public void createNewRole(RoleInfo roleInfo)
        {
            var str = JsonUtility.ToJson(roleInfo);
            mJavaObject.Call("createNewRole", str);
        }

        public void roleLevelUp(RoleInfo roleInfo)
        {
            var str = JsonUtility.ToJson(roleInfo);
            mJavaObject.Call("roleLevelUp", str);
        }

        public void roleSelectServer(RoleInfo roleInfo)
        {
            var str = JsonUtility.ToJson(roleInfo);
            mJavaObject.Call("roleSelectServer", str);
        }

        public void roleExitGame(RoleInfo roleInfo)
        {
            var str = JsonUtility.ToJson(roleInfo);
            mJavaObject.Call("roleExitGame", str);
        }

        public void roleReport(RoleInfo roleInfo)
        {
            var str = JsonUtility.ToJson(roleInfo);
            mJavaObject.Call("roleReport", str);
        }




        public int getChannelId()
        {
            return mJavaObject.Call<int>("getChannelId");
        }

        public string getChannelName()
        {
            return mJavaObject.Call<string>("getChannelName");
        }

        public int getImgId()
        {
            return mJavaObject.Call<int>("getImgId");
        }

        public string getCpsId()
        {
            return mJavaObject.Call<string>("getCpsId");
        }

        public string getChannelSDKVersion()
        {
            return mJavaObject.Call<string>("getChannelSDKVersion");
        }

        public string getUSDKVersion()
        {
            return mJavaObject.Call<string>("getUSDKVersion");
        }

        public string getCustomParams(string key)
        {
            return mJavaObject.Call<string>("getCustomParams", key);
        }

        public string getUniqueId()
        {
            return mJavaObject.Call<string>("getUniqueId");
        }

        public string getTraceID()
        {
            return mJavaObject.Call<string>("getOAID");
        }


        #region call back
        public void init_onSuccess(string none)
        {
            Debug.LogFormat("call event init_onSuccess");
            IQIUSDK.Instance.OnInitSuccess?.Invoke();
        }

        public void init_onFailed(string msg)
        {
            Debug.LogFormat("call event init_onFailed:{0}", msg);
            IQIUSDK.Instance.OnInitFailed?.Invoke(msg);
        }

        public void login_onSuccess(string userJson)
        {
            Debug.LogFormat("call event login_onSuccess:{0}", userJson);
            var userInfo = JsonUtility.FromJson<UserInfo>(userJson);
            IQIUSDK.Instance.OnLoginSuccess?.Invoke(userInfo);
        }

        public void login_onFailed(string msg)
        {
            Debug.LogFormat("call event login_onFailed:{0}", msg);
            IQIUSDK.Instance.OnLoginFailed?.Invoke(msg);
        }

        public void login_onCancel(string none)
        {
            Debug.LogFormat("call event login_onCancel:{0}", none);
            IQIUSDK.Instance.OnLoginCancel?.Invoke();
        }

        public void switch_onSuccess(string userJson)
        {
            Debug.LogFormat("call event switch_onSuccess:{0}", userJson);
            var userInfo = JsonUtility.FromJson<UserInfo>(userJson);
            IQIUSDK.Instance.OnSwitchSuccess?.Invoke(userInfo);
        }

        public void switch_onFailed(string msg)
        {
            Debug.LogFormat("call event switch_onFailed:{0}", msg);
            IQIUSDK.Instance.OnSwitchFailed?.Invoke(msg);
        }

        public void switch_onCancel(string none)
        {
            Debug.LogFormat("call event switch_onCancel:{0}", none);
            IQIUSDK.Instance.OnSwitchCancel?.Invoke();
        }

        public void logout_onSuccess(string none)
        {
            Debug.LogFormat("call event logout_onSuccess:{0}", none);
            IQIUSDK.Instance.OnLogoutSuccess?.Invoke();
        }

        public void logout_onFailed(string msg)
        {
            Debug.LogFormat("call event logout_onFailed:{0}", msg);
            IQIUSDK.Instance.OnLogoutFailed?.Invoke(msg);
        }

        public void logout_onCancel(string none)
        {
            Debug.LogFormat("call event logout_onCancel:{0}", none);
            IQIUSDK.Instance.OnLogoutCancel?.Invoke();
        }

        public void pay_onSuccess(string payJsonData)
        {
            Debug.LogFormat("call event pay_onSuccess:{0}", payJsonData);
            var payData = JsonUtility.FromJson<PayResultData>(payJsonData);
            IQIUSDK.Instance.OnPaySuccess?.Invoke(payData);
        }

        public void pay_onFailed(string payJsonData)
        {
            Debug.LogFormat("call event pay_onFailed:{0}", payJsonData);
            var payData = JsonUtility.FromJson<PayResultData>(payJsonData);
            IQIUSDK.Instance.OnPayFailed?.Invoke(payData);
        }

        public void pay_onCancel(string payJsonData)
        {
            Debug.LogFormat("call event pay_onCancel:{0}", payJsonData);
            var payData = JsonUtility.FromJson<PayResultData>(payJsonData);
            IQIUSDK.Instance.OnPayCancel?.Invoke(payData);
        }

        public void exit_onSuccess(string none)
        {
            Debug.LogFormat("call event exit_onSuccess:{0}", none);
            IQIUSDK.Instance.OnExitSuccess?.Invoke();
        }

        public void exit_onFailed(string msg)
        {
            Debug.LogFormat("call event exit_onFailed:{0}", msg);
            IQIUSDK.Instance.OnExitFailed?.Invoke(msg);
        }

        public void exit_onCancel(string none)
        {
            Debug.LogFormat("call event exit_onCancel:{0}", none);
            IQIUSDK.Instance.OnExitCancel?.Invoke();
        }

        public void extraFunc_onSuccess(string jsonData)
        {
            Debug.LogFormat("call event extraFunc_onSuccess:{0}", jsonData);
            var data = JsonUtility.FromJson<ExtraFuncData>(jsonData);
            IQIUSDK.Instance.OnExtraFuncSuccess?.Invoke(data);
        }

        public void extraFunc_onFailed(string jsonData)
        {
            Debug.LogFormat("call event extraFunc_onFailed:{0}", jsonData);
            var data = JsonUtility.FromJson<ExtraFuncData>(jsonData);
            IQIUSDK.Instance.OnExtraFuncFailed?.Invoke(data);
        }





        #endregion
    }
}